package com.order.controller;

import com.comm.entity.Order;
import com.comm.entity.Product;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
private RestTemplate restTemplate;

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @Resource
    private DiscoveryClient discoveryClient;


    /**
     * 固定一个服务去访问
     * @return
     */
    @GetMapping
    public Order order(){
        // 根据商品的id 查询商品的价格  名字
        // Product localhost:8898/product/2
        Order order = new Order();
        order.setId(1);
        order.setOrdername("testOrder");
        Product forObject = restTemplate.getForObject("http://localhost:9999/product/2", Product.class);
//        System.out.println(forObject);
        order.setProduct(forObject);
        System.out.println(order);
        return order;

    }


    @GetMapping("order1")
    public Order order1(){
        ServiceInstance prodeuct = loadBalancerClient.choose("prodeuct");
        String host = prodeuct.getHost();
        int port = prodeuct.getPort();

        // 根据商品的id 查询商品的价格  名字
        // Product localhost:8898/product/2
        Order order = new Order();
        order.setId(1);
        order.setOrdername("testOrder");
        String url="http://"+host+":"+port+"/product/2";
        String url1 = String.format("http://%s:%s", host,port + "/product/2");
        System.out.println(url);
        Product forObject = restTemplate.getForObject(url, Product.class);
//        System.out.println(forObject);
        order.setProduct(forObject);
        System.out.println(order);
        return order;

    }

    /**
     * DiscoveryClient
     * @return
     */

    @GetMapping("order2")
    public Order order2(){

        List<ServiceInstance> prodeuct1 = discoveryClient.getInstances("prodeuct");
        int i = new Random().nextInt(prodeuct1.size());//2
        ServiceInstance prodeuct = prodeuct1.get(i);
        //  ServiceInstance prodeuct = loadBalancerClient.choose("prodeuct");
        String host = prodeuct.getHost();
        int port = prodeuct.getPort();

        // 根据商品的id 查询商品的价格  名字
        // Product localhost:8898/product/2
        Order order = new Order();
        order.setId(1);
        order.setOrdername("testOrder");
        String url="http://"+host+":"+port+"/product/2";
        String url1 = String.format("http://%s:%s", host,port + "/product/2");
        System.out.println(url);
        Product forObject = restTemplate.getForObject(url, Product.class);
//        System.out.println(forObject);
        order.setProduct(forObject);
        System.out.println(order);
        return order;

    }


}
